package com.cskaoyan.controller.promote;

import com.cskaoyan.bean.Ad;
import com.cskaoyan.bean.BaseListData;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.model.promote.bo.AdUpdateBO;
import com.cskaoyan.model.promote.vo.AdCreateVO;
import com.cskaoyan.service.PromoteService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 推广管理之广告管理
 * http://182.92.235.201:8083/admin/ad/list?page=1&limit=20&sort=add_time&order=desc
 * http://182.92.235.201:8083/admin/ad/list?page=1&limit=20&name=s&content=t&sort=add_time&order=desc
 */
@RestController
@RequestMapping("admin/ad")
public class AdController {

    @Autowired
    PromoteService promoteService;

    /**
     * 查询广告
     */
    @RequestMapping("list")
    @RequiresPermissions(value = {"*", "admin:ad:list"}, logical = Logical.OR)
    public BaseRespVo list(Integer page, Integer limit, String sort, String order, String content,String name){
        BaseListData data = promoteService.queryAds(page, limit, sort, order, content, name);
        return BaseRespVo.ok(data);
    }

    /**
     * 新增广告
     */
    @RequestMapping("create")
    @RequiresPermissions(value = {"*", "admin:ad:create"}, logical = Logical.OR)
    public BaseRespVo create(@RequestBody Ad ad){
        AdCreateVO data = promoteService.createAds(ad);
        return BaseRespVo.ok(data);
    }


    /**
     * 更新广告
     */
    @RequestMapping("update")
    @RequiresPermissions(value = {"*", "admin:ad:delete"}, logical = Logical.OR)
    public BaseRespVo update(@RequestBody AdUpdateBO ad){
        ad.setUpdateTime(new Date());
        promoteService.updateAds(ad);
        return BaseRespVo.ok(ad);
    }

    /**
     * 删除广告
     */
    @RequestMapping("delete")
    @RequiresPermissions(value = {"*", "admin:ad:update"}, logical = Logical.OR)
    public BaseRespVo delete(@RequestBody AdUpdateBO ad){
        promoteService.deleteAds(ad);
        return BaseRespVo.ok();
    }

//    @RequestMapping("storage/create")
//    public String cree(){
//        String s = "{\n" +
//                "\t\"errno\": 0,\n" +
//                "\t\"data\": {\n" +
//                "\t\t\"id\": 17,\n" +
//                "\t\t\"key\": \"0ooyd2wz11ocu1vic0wd.jpg\",\n" +
//                "\t\t\"name\": \"Snipaste_2020-08-01_13-31-35.jpg\",\n" +
//                "\t\t\"type\": \"image/jpeg\",\n" +
//                "\t\t\"size\": 88289,\n" +
//                "\t\t\"url\": \"http://182.92.235.201:8083/wx/storage/fetch/0ooyd2wz11ocu1vic0wd.jpg\",\n" +
//                "\t\t\"addTime\": \"2020-08-01 14:27:45\",\n" +
//                "\t\t\"updateTime\": \"2020-08-01 14:27:45\"\n" +
//                "\t},\n" +
//                "\t\"errmsg\": \"成功\"\n" +
//                "}";
//        return s;
//    }

}
